package bfa.gps;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class MyLocationListener implements LocationListener {

	public Context appCon=null;
	
	public Handler handler=null;
	
	Timer timer = new Timer();
	
	public MyLocationListener(Handler handler) {
	    this.handler=handler;
	    
	    
	    
	    //timer.schedule(new UpdateTimeTask(), 1000, 1000);

	    
	}
	private void notifyMessage(String propName,String str) {
		 if (handler!=null) {
		    Message msg = handler.obtainMessage();
		    Bundle b = new Bundle();
		    b.putString(propName, ""+str);
		    msg.setData(b);
		    handler.sendMessage(msg);
		  }
	 }
	
	
//	public Location location=null;
	
	@Override
	public void onLocationChanged(Location loc) {
//		loc.getLongitude();
//		loc.getLatitude();
//		
//		location.setLatitude(loc.getLatitude());
//		location.setLongitude(loc.getLongitude());
		
		
		//String text="location:   lat="+loc.getLatitude()+" lon:"+loc.getLongitude();
		
		//Toast.makeText( appCon,text,Toast.LENGTH_SHORT).show();
		
		if (loc.hasSpeed()) {
			notifyMessage("speed", new Float(loc.getSpeed()*3.6).toString());	
		}
		
	}
	
	
	class UpdateTimeTask extends TimerTask {
	   public void run() {
		   float r=(System.currentTimeMillis()/1000) % 150;
		   notifyMessage("speed", new Float(r).toString());
	   }
	}

	@Override
	public void onProviderDisabled(String provider) {
		//Toast.makeText(appCon, "Gps Disabilitato", Toast.LENGTH_SHORT).show();
		
		notifyMessage("gpsProvider","disabled");
		
		/*ToggleButton t=(ToggleButton) 
		R.id.satStatus
		*/
	}

	@Override
	public void onProviderEnabled(String provider) {
		//Toast.makeText(appCon, "Gps Abilitato", Toast.LENGTH_SHORT).show();
		
		notifyMessage("gpsProvider","enabled");
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		notifyMessage("gpsStatus", new Integer(status).toString());		
	}

}
